package com.hansheng.ssdxs.ui.activity;

import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.hansheng.ssdxs.R;
import com.hansheng.ssdxs.base.BaseActivity;
import com.hansheng.ssdxs.base.ViewInject;
import com.hansheng.ssdxs.contract.MainContract;
import com.hansheng.ssdxs.presenter.MainPresenter;
import com.hansheng.ssdxs.ui.fragment.FragmentMainHome;
import com.hansheng.ssdxs.ui.fragment.FragmentMainSquare;
import com.hansheng.ssdxs.ui.fragment.FragmentMainUser;
import com.hansheng.ssdxs.view.NoScrollViewPager;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@ViewInject(mainlayoutid = R.layout.activity_main)
public class MainActivity extends BaseActivity implements MainContract.View {

    MainPresenter presenter = new MainPresenter(this);

    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;
    @BindView(R.id.bnve)
    BottomNavigationViewEx bottomNavigationViewEx;

    List<Fragment> fragments;
    private VpAdapter adapter;

    @Override
    public void afterBindView() {
        initViewPager();
        initBottomNavigationViewEx();
    }

    private void initViewPager(){
        // 设置禁止左右滑动
        viewPager.setNoScroll(true);

        fragments = new ArrayList<>();
        FragmentMainHome home = new FragmentMainHome();
        FragmentMainSquare square = new FragmentMainSquare();
        FragmentMainUser user = new FragmentMainUser();
        fragments.add(home);
        fragments.add(square);
        fragments.add(user);

        adapter = new VpAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
    }

    private void initBottomNavigationViewEx(){
//        bottomNavigationViewEx.enableAnimation(false);
        //开始或关闭导航条位移模式。如果为 true，选中项和其他项的宽度不一样。当菜单数大于3时，默认为 true。
        bottomNavigationViewEx.enableShiftingMode(false);
        bottomNavigationViewEx.enableItemShiftingMode(false);

        bottomNavigationViewEx.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.i_home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.i_fabu:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.i_user:
                        viewPager.setCurrentItem(2);
                        break;
                }
                return true;
            }
        });


    }

    @Override
    public void rfTokenSuccess(String token) {

    }

    /**
     * view pager adapter
     */
    private static class VpAdapter extends FragmentPagerAdapter {
        private List<Fragment> data;

        public VpAdapter(FragmentManager fm, List<Fragment> data) {
            super(fm);
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }
    }
}
